<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title></title>
</head>
<body>
	<button type="button" onclick="getdata()">获取数据</button>
	<div class="content"></div>
	
	<script type="text/javascript">
		let con = new document.querySelector('content')
		// function getdata(){
		// 	let xhr = new XMLHttpRequest()
		// 	xhr.open('GET','req.php?username=zhangsan&sex=nv')
		// 	xhr.send()
		// 	xhr.onreadystatechange = () => {
		// 		if(xhr.readyState !==4) return 
		// 		if(xhr.readyState === 4 || xhr.status === 200){
		// 			con.innerHTML = xhr.responseText
		// 		}
		// 	}
		// }
		
		//传统post请求的时候，一般情况下会使用表单，如果使用了表单就要加上请求头
		
		function getdata(){
			let xhr = new XMLHttpRequest()
			xhr.open('POST','req.php')
			xhr.send('username=zhangsan&sex=nv')
			xhr.onreadystatechange = () => {
				if(xhr.readyState !==4) return 
				if(xhr.readyState === 4 || xhr.status === 200){
					con.innerHTML = xhr.responseText
				}
			}
		}
	</script>
</body>
</html>